//
//  MultiCurrencyMoneyUnitTest.swift
//  MultiCurrencyMoneyUnitTest
//
//  Created by lanjing on 2021/3/29.
//

import XCTest
@testable import MultiCurrencyMoney

class MultiCurrencyMoneyUnitTest: XCTestCase {
    func testMultiplication(){
        let five = Money.dollar(amount: 5)
        var product = five.times(2)
        let bank = Bank()
        assert(product.reduced(in: bank, to: "USD") == Money.dollar(amount: 10))
        
        product = five.times(3)
        assert(product.reduced(in: bank, to: "USD") == Money.dollar(amount: 15))
    }
    
    func testEqual(){
        let five1 = Money.dollar(amount: 5)
        let five2 = Money.dollar(amount: 5)
        assert(five1 == five2)
        
        let fiveDollar = Money.dollar(amount: 5)
        let fiveFranc = Money.franc(amount: 5)
        assert(fiveFranc != fiveDollar)
    }

    func testCurrency(){
        assert(Money.dollar(amount: 5).currency == "USD")
        assert(Money.franc(amount: 5).currency == "CHF")
    }
    
    func testSimpleAddition(){
        let fiveDollar = Money.dollar(amount: 5)
        let sum = fiveDollar.plus(Money.dollar(amount: 5))
        let reduced = Bank().reduce(source: sum, to: "USD")
        XCTAssertEqual(reduced, Money.dollar(amount: 10))
    }
    
    func testReduceMoneyDifferentCurrency(){
        let bank = Bank()
        bank.addRate(from:"CHF", to:"USD", rate:2)
        let result = bank.reduce(source: Money.franc(amount: 10), to: "USD")
        XCTAssertEqual(result, Money.dollar(amount: 5))
    }
    
    func testMixedAddition(){
        let fiveDollor = Money.dollar(amount: 5)
        let fiveFranc = Money.franc(amount: 10)
        let bank = Bank()
        bank.addRate(from: "CHF", to: "USD", rate: 2.0)
        
        XCTAssertEqual(Money.dollar(amount: 10), fiveFranc.plus(fiveDollor).reduced(in: bank, to: "USD"))
    }
    
    func testSumMultifier(){
        let twoDollor = Money.dollar(amount: 2)
        let threeDollor = Money.dollar(amount: 3)
        let sum = twoDollor.plus(threeDollor)
        let result = sum.times(3)
        let bank = Bank()
        XCTAssertEqual(result.reduced(in: bank, to: "USD"), Money.dollar(amount: 15))
    }
    
    func testDifferentCurrencySumMultifier(){
        let twoDollor = Money.dollar(amount: 2)
        let threeDollor = Money.franc(amount: 4)
        let sum = twoDollor.plus(threeDollor)
        let result = sum.times(3)
        let bank = Bank()
        bank.addRate(from: "CHF", to: "USD", rate: 2.0)
        XCTAssertEqual(result.reduced(in: bank, to: "USD"), Money.dollar(amount: 12))
    }
}
